/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 *
 * @author Admin
 */
@Entity
@Table(name = "Subject")
@NamedQueries({
    @NamedQuery(name = "Subject.findAll", query = "SELECT s FROM Subject s"),
    @NamedQuery(name = "Subject.findBySubjectId", query = "SELECT s FROM Subject s WHERE s.id = :subjectId"),
    @NamedQuery(name = "Subject.findByName", query = "SELECT s FROM Subject s WHERE s.name = :name"),
    @NamedQuery(name = "Subject.findByFaculty", query = "SELECT s FROM Subject s WHERE s.faculty = :faculty"),
    @NamedQuery(name = "Subject.findByCredits", query = "SELECT s FROM Subject s WHERE s.credits = :credits"),
    @NamedQuery(name = "Subject.findByMaxStudents", query = "SELECT s FROM Subject s WHERE s.maxStudents = :maxStudents"),
    @NamedQuery(name = "Subject.findBySupervisor", query = "SELECT s FROM Subject s WHERE s.supervisor = :supervisor")
})
public class Subject implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "SubjectId")
    private Long id;
    
    @Basic(optional = false)
    @Column(name = "Name")
    private String name;
    
    @Basic(optional = false)
    @Column(name = "MaxStudents")
    private Integer maxStudents;
    
    @Basic(optional = false)
    @Column(name = "Credits")
    private Short credits;
    
    @Column(name = "Version")
    @Version
    int version;
    
    @JoinColumn(name = "FacultyId", referencedColumnName = "FacultyId")
    @ManyToOne(optional = true, cascade = CascadeType.PERSIST)
    private Faculty faculty;
    
    @JoinColumn(name = "ProfessorId", referencedColumnName = "ProfessorId")
    @ManyToOne(optional = true, cascade = CascadeType.PERSIST)
    private Professor supervisor;

    @ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(name = "StudentSubject",joinColumns = {@JoinColumn(name="subjectId") },
        inverseJoinColumns = {@JoinColumn(name="studentId")})
    private List<Student> students;
    
    @ManyToMany(cascade = CascadeType.PERSIST)
    private List<Professor> professors;

    public Subject() {
    }

    public Subject(String name, Integer maxStudents, Short credits) {
        this.name = name;
        this.maxStudents = maxStudents;
        this.credits = credits;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Short getCredits() {
        return credits;
    }

    public void setCredits(Short credits) {
        this.credits = credits;
    }

    public Integer getMaxStudents() {
        return maxStudents;
    }

    public void setMaxStudents(Integer maxStudents) {
        this.maxStudents = maxStudents;
    }

    public Faculty getFaculty() {
        return faculty;
    }

    public void setFaculty(Faculty faculty) {
        this.faculty = faculty;
    }

    public List<Professor> getProfessors() {
        return professors;
    }

    public void setProfessors(List<Professor> professors) {
        this.professors = professors;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public Professor getSupervisor() {
        return supervisor;
    }

    public void setSupervisor(Professor supervisor) {
        this.supervisor = supervisor;
    }
    
    public void addProfessor(Professor professor){
        if(professors == null){
            professors = new ArrayList<Professor>();
        }
        
        professors.add(professor);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addStudent(Student student) {
        if(students == null){
            students = new ArrayList<Student>();
        }
        
        if (!students.contains(student)) {
            students.add(student);
        }
    }

    public void removeStudent(Student student) {
        students.remove(student);
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Subject)) {
            return false;
        }
        Subject other = (Subject) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name + " (" + id + ")";
    }
}
